import Vue from 'vue'
import VueRouter from 'vue-router'
import Main from '../views/Main.vue'
import CategoryEdit from '../views/CategoryEdit'
import CategoryList from '../views/CategoryList'
import ItemEdit  from '../views/ItemEdit'
import ItemList  from '../views/ItemList'
import HeroEdit  from '../views/HeroEdit'
import HeroList  from '../views/HeroList'
import ArticleEdit  from '../views/ArticleEdit'
import ArticleList  from '../views/ArticleList'
import AdEdit  from '../views/AdEdit'
import AdList  from '../views/AdList'
import AdminEdit from '../views/AdminEdit'
import AdminList from '../views/AdminList'
import Login from '../views/Login'

Vue.use(VueRouter)

  const routes = [
    {
      path: '/login',
      name: 'login',
      component: Login
    },
  {
    path: '/',
    name: 'main',
    component: Main,
    children: [
      {
        path: '/categories/add',
        component: CategoryEdit
      },
      {
        path: '/categories/list',
        component: CategoryList
      },
      {
        path: '/categories/edit/:id',
        component: CategoryEdit,
        props: true
      },
      {
        path: '/items/add',
        component: ItemEdit,
      },
      {
        path: '/items/edit/:id',
        component: ItemEdit,
        props: true
      },
      {
        path: '/items/list',
        component: ItemList
      },
      {
        path: '/heroes/add',
        component: HeroEdit,
      },
      {
        path: '/heroes/edit/:id',
        component: HeroEdit,
        props: true
      },
      {
        path: '/heroes/list',
        component: HeroList
      },
      {
        path: '/articles/add',
        component: ArticleEdit
      },
      {
        path: '/articles/edit/:id',
        component: ArticleEdit,
        props: true
      },
      {
        path: '/articles/list',
        component: ArticleList
      },
      {
        path: '/ads/add',
        component: AdEdit
      },
      {
        path: '/ads/edit/:id',
        component: AdEdit,
        props: true
      },
      {
        path: '/ads/list',
        component: AdList
      },
      {
        path: '/admins/add',
        component: AdminEdit
      },
      {
        path: '/admins/edit/:id',
        component: AdminEdit,
        props: true
      },
      {
        path: '/admins/list',
        component: AdminList
      },
    ]
  },
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next();
  const token = window.localStorage.getItem('token');
  if (!token) return next('/login');
  next();
});

export default router
